// 网关配置的前缀
var jobsServer = "/jobsSer"

// let hrId = "580124";
// let hrName = "OVERTONPERSIS";
let hrId = "";
let hrName = "";


let minSalary_value = "";
let maxSalary_value = "";

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    let accountStatus = $("#accountStatus").html();
    if(accountStatus !== "正常"){
        $("#forbidden").removeAttr("hidden");
    }

    let authorities = $("#authorities").html();
    unauthorized_2(authorities, "postJob");

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        jobsServer = data.jobsServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    hrId = $("#userId").html();
    hrName = $("#userName").html();
    getJobType();
    getSalaryUnit();
    getExperience();
    getEducation();
}

function postJob() {

    let jobName = $("#jobName").val();
    let salaryMin = $("#salaryMin").val();
    let salaryMax = $("#salaryMax").val();
    let industry = $("#industry").val();
    let introduce = $("#introduce").val();
    let requirement = $("#requirement").val();
    let skills = $("#skills").val();

    let salaryError = $('#salary-error').html();

    if ((jobName === "" || jobName === null) ||
        (salaryMin === "" || salaryMin === null) ||
        (salaryMax === "" || salaryMax === null) ||
        (industry === "" || industry === null) ||
        (introduce === "" || introduce === null) ||
        (requirement === "" || requirement === null) ||
        (skills === "" || skills === null) ||
        (salaryError !== "" && salaryError !== null)) {
        $.confirm({
            title: '警告',
            content: '工作内容存在未填数据<br/>',
            type: 'green',
            theme: "modern",
            closeAnimation: 'scaleY',
            buttons: {
                cancel: {
                    text: '关闭',
                    btnClass: 'btn-default'
                }
            }
        });
    } else {
        let jobType = $('#jobType .current').html();
        let salaryUnit = $('#salary_util .current').html();
        let experience = $('#experienceList .current').html();
        let education = $('#educationList .current').html();

        $.confirm({
            title: '提示',
            content: '确定发布该工作吗？<br/>',
            type: 'green',
            theme: "modern",
            closeAnimation: 'scaleY',
            buttons: {
                ok: {
                    text: '确认',
                    btnClass: 'btn-warning',
                    action: function () {
                        $.ajax({
                            type: "PUT",
                            async: false,
                            url: jobsServer + "/jobs/jobInfo",
                            headers: {
                                "Content-Type": "application/json",
                                "Authorization": Authorization,
                                "refreshToken": refreshToken,
                            },
                            data: JSON.stringify({
                                "hrId": hrId,
                                "hrName": hrName,

                                "jobName": jobName,
                                "salaryMin": salaryMin,
                                "salaryMax": salaryMax,
                                "industry": industry,
                                "introduce": introduce,
                                "requirement": requirement,
                                "skills": skills,
                                "jobType": jobType,
                                "salaryUnit": salaryUnit,
                                "experience": experience,
                                "education": education,
                            }),
                            success: function (data) {
                                if (data.resp_code === 0) {
                                    // 成功
                                    cocoMessage.info(2000, data.resp_msg, function () {
                                        console.log(data.resp_msg);
                                    });

                                    $("#jobName").val("");
                                    $("#salaryMin").val("");
                                    $("#salaryMax").val("");
                                    $("#industry").val("");
                                    $("#introduce").val("");
                                    $("#requirement").val("");
                                    $("#skills").val("");
                                } else {
                                    // 失败
                                    cocoMessage.error(2000, data.resp_msg, function () {
                                        console.log(data.resp_msg);
                                    });
                                }
                            },
                            error: function (err) {
                                // 异常
                                cocoMessage.error(2000, err.statusText, function () {
                                    console.log(err);
                                });
                            }
                        })
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'btn-default'
                }
            }
        });
    }
}


// 获得 工作类别
function getJobType() {
    let $list = $("#jobType .list");
    let $mySelect = $("#jobType .mySelect");
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/jobType",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("工作类别");

            // 如果为空
            if (data.datas.length <= 0) {
                $mySelect.empty();
                $mySelect.append(
                    "<option>暂无数据</option>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                if (i === 0) {
                    $('#jobType .current').html(item.value);
                }
                $list.append(
                    "<li data-value='" + item.value + "' class='option'>" + item.value + "</li>"
                );
            });
        },
        error: function (err) {
            $mySelect.empty();
            $mySelect.append(
                "<option>暂无数据</option>"
            );
        }

    });
}

// 获得 工资单位
function getSalaryUnit() {
    let $list = $("#salary_util .list");
    let $mySelect = $("#salary_util .mySelect");
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/salaryUnit",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("工资单位");
            // 如果为空
            if (data.datas.length <= 0) {
                $mySelect.empty();
                $mySelect.append(
                    "<option>暂无数据</option>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                if (item.value === "千/月") {
                    $('#salary_util .current').html("千/月");
                }
                $list.append(
                    "<li data-value='" + item.value + "' class='option'>" + item.value + "</li>"
                );
            });
        },
        error: function (err) {
            $mySelect.empty();
            $mySelect.append(
                "<option>暂无数据</option>"
            );
            console.log(err);
        }

    });
}

// 获得 工作经验
function getExperience() {
    let $list = $("#experienceList .list");
    let $mySelect = $("#experienceList .mySelect");
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/experience",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("工作经验");

            // 如果为空
            if (data.datas.length <= 0) {
                $mySelect.empty();
                $mySelect.append(
                    "<option>暂无数据</option>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                if (i === 0) {
                    $('#experienceList .current').html(item.value);
                }
                $list.append(
                    "<li data-value='" + item.value + "' class='option'>" + item.value + "</li>"
                );
            });
        },
        error: function (err) {
            $mySelect.empty();
            $mySelect.append(
                "<option>暂无数据</option>"
            );
        }

    });
}

// 获得 学历要求
function getEducation() {
    let $list = $("#educationList .list");
    let $mySelect = $("#educationList .mySelect");
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/educationLevel",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("学历要求");

            // 如果为空
            if (data.datas.length <= 0) {
                $mySelect.empty();
                $mySelect.append(
                    "<option>暂无数据</option>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                if (i === 0) {
                    $('#educationList .current').html(item.value);
                }
                $list.append(
                    "<li data-value='" + item.value + "' class='option'>" + item.value + "</li>"
                );
            });
        },
        error: function (err) {
            $mySelect.empty();
            $mySelect.append(
                "<option>暂无数据</option>"
            );
        }

    });
}

//  最低工资
function click_minSalary() {
    var tmp = $('#salaryMin').val();

    if (!salary_error(tmp)) {
        $('#salaryMin').val('');
        return;
    }
    if (tmp < 0) {
        $('#salary-error').append('请输入大于0的数');
    }
    if (maxSalary_value != 0 && maxSalary_value <= tmp) {
        $('#salary-error').append('请输入小于最大值的数');
        $('#salaryMin').val('');
        return;
    }
    minSalary_value = tmp;
}

//  最高工资
function click_maxSalary() {
    var tmp = $('#salaryMax').val();

    if (!salary_error(tmp)) {
        $('#salaryMax').val('');
        return;
    }
    if (minSalary_value != 0 && minSalary_value >= tmp) {
        $('#salary-error').append('请输入大于最小值的数');
        $('#salaryMax').val('');
        return;
    }
    maxSalary_value = tmp;
}

function salary_error(tmp) {
    $('#salary-error').html('');
    var reg = /^[0-9]+.?[0-9]*$/;
    var pattern = new RegExp(reg);
    if (!pattern.test(tmp)) {
        $('#salary-error').append('请输入数值类型');
        return false;
    } else {
        return true;
    }
}
